package com.ruoyi.web.controller.common;

import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONUtil;
import com.google.zxing.WriterException;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.page.ResponseResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.GoogleAuthenticationTool;
import com.ruoyi.system.service.ISysUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.io.IOException;

@Api(tags = "谷歌验证绑定")
@RestController
@RequestMapping("common")
public class GoogleController extends BaseController {


    @Resource
    private ISysUserService userService;


    private SysUser getSysUser() {
        Long userId = getUserId();
        SysUser user = userService.getById(userId);
        return user;
    }

    @ApiOperation(value = "获取二维码信息")
    @GetMapping("getCode")
    public ResponseResult<QrCodeVo> getCode() {
        QrCodeVo qrCodeVo = new QrCodeVo();
        SysUser user = getSysUser();

        if (StrUtil.isNotBlank(user.getRandomSecretKey())) {
            return setSuccessResult(qrCodeVo);
        }


        String randomSecretKey = GoogleAuthenticationTool.generateSecretKey();
        //此步设置的参数就是App扫码后展示出来的参数
        String qrCodeString = GoogleAuthenticationTool.spawnScanQRString(user.getUserName(), randomSecretKey, "zhaocaimao");
        String qrCodeImageBase64 = null;
        try {
            qrCodeImageBase64 = GoogleAuthenticationTool.createQRCode(qrCodeString, null, 512, 512);
        } catch (WriterException | IOException e) {
            e.printStackTrace();
        }
        qrCodeVo.setRandomSecretKey(randomSecretKey);
        qrCodeVo.setQrCodeImageBase64(qrCodeImageBase64);
        return setSuccessResult(qrCodeVo);
    }

    @Log(title = "绑定谷歌", businessType = BusinessType.UPDATE)
    @ApiOperation(value = "绑定验证码")
    @PostMapping("bind")
    public ResponseResult bind(@Valid @RequestBody BindCodeDto bindCodeDto) {
        System.out.println(JSONUtil.toJsonStr("参数:" + bindCodeDto));

        SysUser user = getSysUser();
        if (StrUtil.isNotBlank(user.getRandomSecretKey())) {
            return this.setErrorResult("已绑定");
        }
        String rightCode = GoogleAuthenticationTool.getTOTPCode(bindCodeDto.getRandomSecretKey());
        System.out.println("结果:" + rightCode);
        if (!StrUtil.equals(rightCode, bindCodeDto.getGoogleCode())) {
            return this.setErrorResult("验证码有误");
        }
        user.setRandomSecretKey(bindCodeDto.getRandomSecretKey());
        userService.updateDomain(user);
        return setSuccessResult(user);
    }


//    @ApiOperation(value = "验证是否正确")
//    @PostMapping("test")
//    public ResponseResult test(@RequestBody BindCodeDto bindCodeDto) {
//        SysUser user = getSysUser();
//        boolean b = userService.checkCode(user, bindCodeDto.getGoogleCode());
//        return setSuccessResult(b);
//    }


//    @ApiOperation(value = "修改密码")
//    @PostMapping("updatePwd")
//    public ResponseResult updatePwd(@Valid @RequestBody UpdatePwdDto updatePwdDto) {
//        SysUser user = getSysUser();
//        userService.checkCode(user, updatePwdDto.getGoogleCode());
//        userService.resetPwd(user.getId(), updatePwdDto.getPwd());
//        return setSuccessResult();
//    }
}
